<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="120" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addMainGroup, editMainGroup } from '@/api/repair/repair/labourGroup';

export default {
  name: 'OrderModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.mainGroupCode;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '项目车型组',
          fieldName: 'modelLabourCode',
          placeholder: '请选择',
          itemList: []
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },

        {
          type: 'SELECT',
          label: '主分类名称',
          fieldName: 'primaryGroupName',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修项目代码',
          fieldName: 'labourCode',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色代码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '维修项目类型',
          fieldName: 'repairType',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修项目名称',
          fieldName: 'labourName',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色代码', trigger: 'blur' }]
        },

        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'SpellCode',
          initialValue: '',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '工种',
          fieldName: 'workerTypeCode',
          placeholder: '请选择',
          itemList: []
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '标准工时',
          fieldName: 'stdLabourHour',
          initialValue: '',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '派工工时',
          fieldName: 'assignLabourHour',
          initialValue: '',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '索赔工时',
          fieldName: 'claimLabour',
          initialValue: '',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '总部标准工时',
          fieldName: 'oemLabourHour',
          initialValue: '',
          placeholder: ''
          // rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addMainGroup(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editMainGroup(this.recordId, this.saveData);
        }
        // if (res.resultCode === 200) {
        this.closeDialog('refresh');
        // }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
